//
//  FTPhotoCropCoverView.m
//  Fetion
//
//  Created by jiazhenxiu on 14-5-8.
//  Copyright (c) 2014年 chinasofti. All rights reserved.
//

#import "FTPhotoCropCoverView.h"

@implementation FTPhotoCropCoverView

- (id)initWithFrame:(CGRect)frame andSelectStyle:(ZSelectImageStyle)style
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectStyle = style;
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
//        self.alpha = 0.3;
    }
    return self;
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return NO;
}

- (void)drawRect:(CGRect)rect
{
    if (self.selectStyle == ZSelectImage_RectStyle)
    {
        int gap = 1;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.4);
        CGContextFillRect(context, rect);
        CGFloat y = (rect.size.height - CropHeadImageWidth - 64)/2;
        CGFloat x = (rect.size.width - CropHeadImageWidth)/2;
        CGContextSetRGBFillColor(context, 255, 255, 255, 0.6);
        CGContextFillRect(context, CGRectMake(x,y,CropHeadImageWidth,CropHeadImageWidth));
        CGContextClearRect(context, CGRectMake(x+gap,y+gap,CropHeadImageWidth-gap*2,CropHeadImageWidth-gap*2));
        CGContextStrokePath(context);
    }
    else
    {
        CGFloat kWidthGap = 0;
        CGFloat kHeightGap = 0;
        CGFloat width = rect.size.width;
        CGFloat height = rect.size.height;
        //pickingFieldWidth:圆形框的直径
        CGFloat pickingFieldWidth = width < height ? (width - kWidthGap) : (height - kHeightGap);
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        CGContextSaveGState(contextRef);
        CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.35);
        CGContextSetLineWidth(contextRef, 3);
        //计算圆形框的外切正方形的frame：
        CGRect pickingFieldRect = CGRectMake((width - pickingFieldWidth) / 2, (height - pickingFieldWidth - 64.0f) / 2, pickingFieldWidth, pickingFieldWidth);
        //创建圆形框UIBezierPath:
        UIBezierPath *pickingFieldPath = [UIBezierPath bezierPathWithOvalInRect:pickingFieldRect];
        //创建外围大方框UIBezierPath:
        UIBezierPath *bezierPathRect = [UIBezierPath bezierPathWithRect:rect];
        //将圆形框path添加到大方框path上去，以便下面用奇偶填充法则进行区域填充：
        [bezierPathRect appendPath:pickingFieldPath];
        //填充使用奇偶法则
        bezierPathRect.usesEvenOddFillRule = YES;
        [bezierPathRect fill];
        CGContextSetLineWidth(contextRef, 2);
        CGContextSetRGBStrokeColor(contextRef, 255, 255, 255, 1);
        //设置贝塞尔曲线的线条间隔
        CGFloat dash[2] = {0,0};
        [pickingFieldPath setLineDash:dash count:2 phase:0];
        [pickingFieldPath stroke];
        CGContextRestoreGState(contextRef);
        self.layer.contentsGravity = kCAGravityCenter;
    }
}

@end




